﻿# -*- coding:utf-8 -*-
import os
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from homesite.images.utils import thumbnail



class Picture(models.Model):
    title = models.CharField(max_length=64, verbose_name='Название')
    file = models.ImageField(upload_to='files', verbose_name='Файл')
    description =  models.TextField(blank=True, verbose_name='Описание')
    date = models.DateTimeField(auto_now_add=True, verbose_name='Дата')
    user = models.ForeignKey(User, verbose_name="Пользователь")
    
    
    class Meta:
        ordering = ['-date']

    def __unicode__(self):
        return self.title
 
    def preview_image_url(self):
        image_path = thumbnail(self.file, '90x90')
        return '<a href="'+ str(self.id) +'/"><img src="' + str(image_path) +'"/></a>'

    preview_image_url.short_description = 'Картинка'
    preview_image_url.allow_tags = True

    def image_url(self):
        image_path = thumbnail(self.file, '300x300')
        return '<img src="' + str(image_path) +'"/>'